<template>
    <div>
        <el-form ref="search" :model="search" label-width="80px">
            <el-col :span="8">
                &nbsp;
            </el-col>
            <el-form-item label="患者姓名">
                <el-col :span="5">
                    <el-input v-model="search.name" placeholder="患者姓名"></el-input>
                </el-col>
                <el-col :span="1">
                    <el-button @click="getSearch()"><i class="el-icon-search"></i></el-button>
                </el-col>
            </el-form-item>
        </el-form>

        <el-table :data="inpatientList" style="width: 100%" border>
            <el-table-column prop="inpid" label="编号"> </el-table-column>
            <el-table-column prop="pname" label="姓名"> </el-table-column>
            <el-table-column prop="psex" label="性别"> </el-table-column>
            <el-table-column prop="kname" label="科室"> </el-table-column>
            <el-table-column prop="srid" label="病房号"> </el-table-column>
            <el-table-column prop="bname" label="病床编号"> </el-table-column>
            <el-table-column prop="indate" label="入院时间"> </el-table-column>
            <el-table-column label="操作" width="275px" fixed="right" align="center">
                <template slot-scope="scope">
                    <el-button size="mini" type="danger" @click="remo(scope.row)">出院</el-button>
                    <el-button size="mini" @click="recordRoleShow(scope.row)" type="success">用药记录</el-button>
                    &nbsp;
                    <router-link to="/patientIn">
                        <el-button size="mini" type="primary" @click="service(scope.row)">服务</el-button>
                    </router-link>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页功能 -->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="pageInfo.pageNum" :page-sizes="[5, 10, 15, 20]" :page-size="pageInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="pageInfo.total">
        </el-pagination>

        <el-dialog title="用药记录" :visible.sync="recordDialogVisible" width="40%" :before-close="handleClose"
            style="text-align: left">
            <el-table :data="recordList" style="width: 100%" border>
                <el-table-column prop="reid" label="编号" width="50px"> </el-table-column>
                <el-table-column prop="pname" label="患者姓名"> </el-table-column>
                <el-table-column prop="kname" label="科室"> </el-table-column>
                <el-table-column prop="dname" label="主治医生"> </el-table-column>
                <el-table-column prop="mdname" label="药品名称"> </el-table-column>
                <el-table-column prop="rdnum" label="药品数量"> </el-table-column>
                <el-table-column prop="redata" label="开药时间"> </el-table-column>
            </el-table>
            <el-form ref="record" :model="record" label-width="80px" :rules="rules">
                <el-col :span="8">
                    <el-form-item label="药品" prop="medid">
                        <el-select v-model="record.medid" placeholder="药品名称" @change="getRdnums()">
                            <el-option v-for="medicion in medicionList" :key="medicion.medid" :label="medicion.mdname"
                                :value="medicion.medid">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="药品库存" prop="mdnum">
                        <el-input disabled type="number" v-model="record.mdnum" placeholder="药品库存"
                            id="mdnumid"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="药品数量" prop="rdnum">
                        <el-input @input="rdnumUpdate()"  v-model="record.rdnum"
                            placeholder="药品数量" id="rdnumid"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item>
                        <el-button type="primary" @click="intorecord()">立即创建</el-button>
                    </el-form-item>
                </el-col>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="assignRole()">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog title="出院" :visible.sync="remoDialogVisible" width="40%" :before-close="handleCloses"
            style="text-align: left">
            <el-table :data="inpatientList2" style="width: 100%" border>
                <el-table-column prop="inpid" label="编号"> </el-table-column>
                <el-table-column prop="pname" label="姓名"> </el-table-column>
                <el-table-column prop="psex" label="性别"> </el-table-column>
                <el-table-column prop="kname" label="科室"> </el-table-column>
                <el-table-column prop="srid" label="病房号"> </el-table-column>
                <el-table-column prop="bname" label="病床编号"> </el-table-column>
            </el-table>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="remoY()">确 定 出 院</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
//导出
export default {
    data() {
        var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('数量不能为空'));
        }
            if (value <=0) {
              callback(new Error('请开正常数量的药'));
            } else {
              callback();
            }
      };
        return {
            //搜索栏
            search: {},
            //获得所有的住院的角色
            inpatientList: [],
            //获得要出院的角色
            inpatientList2: [],
            //单个住院内容
            inpatient: {},
            //用药记录
            recordDialogVisible: false,
            remoDialogVisible: false,
            //用药记录表
            record: {},
            //用药记录表集合
            recordList: [],
            //医生集合
            doctorList: [],
            //药品集合
            medicionList: [],
            //分页
            pageInfo: {
                pageNum: 1,
                pageSize: 5
            },
            //校验
            rules: {
                medid: [
                    { required: true, message: '请选择药品', trigger: 'change' }
                ],
                rdnum: [
                    { validator: checkAge, trigger: 'blur' }
                ]
            },
            //药品对象
            medicions: {},
        };

    },
    methods: {
        
        //改变库存
        rdnumUpdate() {
            document.getElementById("mdnumid").value = this.record.mdnum;
            document.getElementById("mdnumid").value -= this.record.rdnum;
        },
        //获取对应的库存
        getRdnums() {
            this.$axios.get("medicion/findMedid?medid=" + this.record.medid)
                .then((result) => {
                    let vo = result.data;
                    if (vo.code == 200) {
                        document.getElementById("mdnumid").value = vo.data.mdnum;
                        this.record.mdnum = vo.data.mdnum;
                        this.medicions = vo.data;
                    }
                }).catch((err) => {
                    console.log(err);
                });
        },
        //退院界面
        remo(row) {
            this.inpatientList2 = [];
            this.inpatient = row;
            this.inpatientList2[0] = row;
            this.remoDialogVisible = true;
        },
        //确认退院
        remoY() {
            this.$axios.post("inpatient/remo", this.inpatient)
                .then((result) => {
                    let vo = result.data;
                    this.$message(vo.message);
                    if (vo.code == 200) {
                        this.remoDialogVisible = false;
                        this.list();
                    }
                }).catch((err) => {
                    console.log(err);
                });
        },
        //跳转页面，传值
        service(row) {
            this.$axios.get("inpatient/getInpid?inpid=" + row.inpid)
                .then((result) => {
                    let vo = result.data;
                    this.$message(vo.message);
                    if (vo.code == 200) {
                    }
                }).catch((err) => {
                    console.log(err);
                });
        },
        //添加用药记录
        intorecord() {
            this.$refs['record'].validate((valid) => {
                if (valid) {
                    this.record.mdnum = document.getElementById("mdnumid").value;
                    if (this.record.mdnum < 0) {
                        this.$message("库存不足");
                        this.getRdnums();
                        this.$refs['record'].resetFields();
                    } else {
                        this.medicions.mdnum = this.record.mdnum;
                        this.$axios.put("/medicion/updatenum", this.medicions)
                            .then((result) => {
                                this.$message(result.data.message);
                                this.DialogFormVisible = false;
                                this.list();
                            }).catch((err) => {
                                console.log(err);
                            });
                        this.$axios.post("record/insert", this.record)
                            .then((result) => {
                                let vo = result.data;
                                this.$message(vo.message);
                                if (vo.code == 200) {
                                    this.$axios.get("asset/updateMe?pid=" + this.record.pid + "&cost=" + (this.record.rdnum*this.medicions.price))
                                    .then((result) => {
                                        let vo = result.data;
                                        this.$message(vo.message);
                                        if (vo.code == 200) {
                                        }
                                    }).catch((err) => {
                                        console.log(err);
                                    });
                                    this.$axios.get("record/findPid?pid=" + this.record.pid)
                                        .then((result) => {
                                            let vo = result.data;
                                            if (vo.code == 200) {
                                                this.recordList = vo.data;
                                                this.$refs['record'].resetFields();
                                            }
                                        }).catch((err) => {
                                            console.log(err);
                                        });
                                }
                            }).catch((err) => {
                                console.log(err);
                            });

                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        //关闭模态框
        assignRole() {
            this.$refs['record'].resetFields();
            this.recordDialogVisible = false;
        },
        //查询
        getSearch() {
            if (this.search.name == "") {
                this.list();
            } else {
                this.$axios.get("inpatient/getSearch?pageNum=" + this.pageInfo.pageNum + "&pageSize=" + this.pageInfo.pageSize + "&name=" + this.search.name)
                    .then((result) => {
                        let vo = result.data;
                        if (vo.code == 200) {
                            this.inpatientList = vo.data.list;
                        }
                    }).catch((err) => {
                        console.log(err);
                    });
            }
        },
        //打开用药记录模态框
        recordRoleShow(row) {
            this.$axios.get("record/findPid?pid=" + row.pid)
                .then((result) => {
                    let vo = result.data;
                    if (vo.code == 200) {
                        document.getElementById("rdnumid").value=0;
                        this.recordList = vo.data;
                    }
                }).catch((err) => {
                    console.log(err);
                });
            this.record.pid = row.pid;
            this.record.inpid = row.inpid;
            this.recordDialogVisible = true;
        },
        //分页
        handleSizeChange(val) {
            this.pageInfo.pageSize = val;
            this.list();
        },
        handleCurrentChange(val) {
            this.pageInfo.pageNum = val;
            this.list();
        },
        //添加用药记录
        onSubmit() {
            console.log('submit!');
        },
        handleCloses() {
            this.remoDialogVisible = false;
        },
        //点叉号或空白位置关闭模态框
        handleClose() {
            this.$refs['record'].resetFields();
            this.recordDialogVisible = false;
        },
        list() {
            //查所有住院病人
            this.$axios.get("inpatient/getAll?pageNum=" + this.pageInfo.pageNum + "&pageSize=" + this.pageInfo.pageSize)
                .then((result) => {
                    let vo = result.data;
                    if (vo.code == 200) {
                        this.inpatientList = vo.data.list;
                        this.pageInfo = vo.data;
                    }
                }).catch((err) => {
                    console.log(err);
                });
            //查询所有药品
            this.$axios.get("medicion/findAll1")
                .then((result) => {
                    let vo = result.data;
                    if (vo.code == 200) {
                        this.medicionList = vo.data;
                    }
                }).catch((err) => {
                    console.log(err);
                });
        },
    },
    mounted() {
        
        this.list();
    },
};
</script>
<style></style>